<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.w3.org/1999/xhtml">

<head>
    <th:block th:include="common/common_head :: commonHeader('扣款单')"/>
    <link rel="stylesheet" th:href="@{/static/js/plugins/jquery-layout/jquery.layout-latest.css}">
    <style type="text/css">
        .la{text-align:left;}
        .textBox{text-align:right;}
    </style>
</head>

<body class="gray-bg">

<div class="container-div ui-layout-center">
    <div class="row">
        <div class="col-sm-12 search-condition">
            <form id="materialFactoryForm" th:action="@{/materialFactory/list}" class="form-inline">
                <div class="select-list">
                    <ul>
                        <li class="col-sm-3">
                            <label>物料号: </label>
                            <input type="text" name="materialCode"/>
                        </li>
                        <li class="col-sm-3">
                            <label>物料描述：</label>
                            <input type="text" name="materialDesc"/>
                        </li>
                        <li class="col-sm-3">
                            <label>物料状态：</label>
                            <input type="text" name="crossMaterialStatus"/>
                        </li>
                        <li class="col-sm-3">
                            <div>
                                <label>工厂：</label>
                                <input type="text" name="werks"/>
                            </div>
                        </li>
                        <li class="col-sm-3">
                            <div>
                                <label>库存地点：</label>
                                <input type="text" name="storageLocation"/>
                            </div>
                        </li>

                        <li class="col-sm-3">
                            <div>
                                <label>采购组织：</label>
                                <input type="text" name="purchaseGroup"/>
                            </div>
                        </li>
                        <li class="col-sm-3">
                            <div>
                                <label>库存：</label>
                                <select id="kc" name="kc">
                                    <option value="0">有库存</option>
                                    <option value="1">无库存</option>
                                    <option value="2">全部</option>
                                </select>

                            </div>
                        </li>

                        <li class="col-sm-3">
                            <button id="materialFactorySearchBtn" type="button"
                                    class="btn btn-primary btn-rounded btn-sm"><i class="fa fa-search"></i>&nbsp;搜索
                            </button>
                            <button class="btn btn-warning btn-rounded btn-sm" type="reset"><i class="fa fa-eraser"></i>&nbsp;重置
                            </button>
                            <button id="materialFactoryAdvancedSearch" type="button"
                                    class="btn btn-primary btn-rounded btn-sm" style="display: none"><i
                                    class="fa fa-caret-down"></i>&nbsp;高级搜索
                            </button>
                        </li>
                    </ul>
                </div>
            </form>
        </div>
        <div class="col-sm-12 select-info order-table">
            <div class="btn-group hidden-xs toolbar" id="materialFactoryToolBar" role="group">
                <a class="btn btn-outline btn-success btn-rounded" onclick="toLead()">
                    <i class="fa fa-plus"></i> 申请
                </a>
                <a class="btn btn-outline btn-success btn-rounded" onclick="toLeadE()">
                    <i class="fa fa-plus"></i> 编辑
                </a>
                <a class="btn btn-outline btn-success btn-rounded" onclick="toLeadD()">
                    <i class="fa fa-plus"></i> 领料单 <span id="iconNum" class="required-sign">0</span>
                </a>
                <i class="fa fa-plus"></i> 领料人: <input style="height: 20px" type="text" id="leadMan" name="leadMan"/>
            </div>
            <div>
                <table id="materialFactory" data-mobile-responsive="true">
                </table>
            </div>
        </div>
    </div>
</div>
<!-- 引入公共js -->
<div th:include="common/onload_js :: onloadJS"></div>
<script type="text/javascript" th:src="@{/static/js/plugins/jquery-layout/jquery.layout-latest.js}"></script>

<!-- Page-Level Scripts -->
<script type="text/javascript">

    //面向对象
    var searcherBS = Searcher.createBS('#materialFactory',
        [
            {checkbox: true},
            {title: '物料号', field: 'materialCode'},
            {title: '物料状态', field: 'crossMaterialStatus'},
            {title: '物料描述', field: 'materialDesc'},
            {title: '有效日期 ', field: 'beginDate'},
            {title: '工厂', field: 'werks'},
            {title: '库存地点', field: 'storageLocation'},
            {title: '采购组', field: 'purchaseGroup'},
            {title: '维护状态', field: 'status'},
            {title: '库存数量', field: 'qunatity'},
            {title: '安全库存数量', field: 'safeCont'},
            {title: '领用次数', field: 'isLead'},
            {title: '已用库存', field: 'usecont'},
            {title: '备注', field: 'remark'},
            {
                title: '可用库存', formatter: function (value, row, index) {
                    return row.qunatity - row.usecont;
                }
            },
            {title: '储位', field: 'storage'},
            {
                title: '状态', formatter: function (value, row, index) {
                    var apDays = row.qunatity - row.safeCont;
                    if (apDays <= 0) {//当数量小于安全库存亮红灯
                        return "<img src='" + ctxPath + "/img/light/red.png' width='20px' style='margin-right:5px' />";
                    } else {//还早着呢，亮绿灯
                        return "<img src='" + ctxPath + "/img/light/green.png' width='20px' style='margin-right:5px' />";
                    }
                    return apDays;
                }
            }
        ]
    );

    $(function () {
        Page.ajaxPOST(ctxPath + '/materialFactory/selectNum', null, function (res) {
            //设置角标数量
            $("#iconNum").text(res.result);
            //           parent.refresh();
            //           Dialog.closeFrame(window.name);
        });
    });

    //刷新数据
    function refresh() {
        searcherBS.refresh();
    }

    // 初始化时间插件
    laydate.render({elem: '#beginDate'});

    initDictDataSel();//初始化所有字典数据
    /**
     * @Description: 跳转领用界面
     */
    function toLead() {
        //获取工厂id
        var rowDatas = searcherBS.getSelectedRows("materialFactoryId");
        var materialCode = searcherBS.getSelectedRows("materialCode");
        var usecont = searcherBS.getSelectedRows("usecont");
        var qunatity = searcherBS.getSelectedRows("qunatity");
        var leadMan = $("#leadMan").val().trim();
        if (leadMan == null || leadMan == "") {
            Dialog.errorMsg("领料人不可以为空!");
            return;
        }
        //截取料号首字母
        materialCode = materialCode.toString().substring(0, 1);
        //用料号首字母判断是不是新建料号,不是新建料号是没有库存的
        if (materialCode != "N") {
            Dialog.errorMsg("不是新建料号,无法进行该操作!");
            return;
        }
        if (qunatity - usecont == 0) {
            Dialog.errorMsg("当前库存为0,无法进行该操作!");
            return;
        }
        Page.ajaxPOST(ctxPath + '/materialFactory/toLead/' + rowDatas + "/" + leadMan, null, function (res) {
            Dialog.msg("添加成功!", function () {
                var iconNum = $("#iconNum").text();
                $("#iconNum").text(parseInt(iconNum) + rowDatas.length);
                parent.refresh();
                Dialog.closeFrame(window.name);
            });
        }, {"loadingMsg" : "正在往服务器提交数据……"});
    }

    /**
     * @Description: 跳转物料申请界面
     */
    function toLeadD() {
        var iconNum = $("#iconNum").text();
        if (iconNum == 0) {
            Dialog.errorMsg("还没有领用物料,请先领用物料!");
            return;
        }
        var index = Dialog.openUrl("物料领用", ctxPath + '/materialFactory/toLeadD', {
            area: ['900px', '700px'],
            maxmin: true,
            end: function () {
            }
        });
        layer.full(index);
    }

    /**
     * @Description: 删除角标
     */
    function deleteNum(index) {
        var iconNum = $("#iconNum").text();
        $("#iconNum").text(parseInt(iconNum) - index);
    }

    /**
     * @Description: 编辑状态
     */
    function toLeadE() {
        //获取工厂id
        var materialFactoryId = searcherBS.getSelectedRows("materialFactoryId");
        var materialCode = searcherBS.getSelectedRows("materialCode");
        if (materialFactoryId.length != 1) {
            Dialog.errorMsg("请选择一行进行编辑");
            return;
        }
        //截取料号首字母
        materialCode = materialCode.toString().substring(0, 1);
        //用料号首字母判断是不是新建料号,不是新建料号是没有库存的
        if (materialCode != "N") {
            Dialog.errorMsg("不是新建料号,无法进行该操作!");
            return;
        }
        var index = Dialog.openUrl("物料工厂数据编辑", ctxPath + '/materialFactory/toLeadE/' + materialFactoryId, {
            area: ['900px', '700px'],
            maxmin: true,
            end: function () {
            }
        });
        layer.full(index);
    }
</script>
</body>
</html>